package com.chenlixian.fishandbearpaw.controller;

import com.chenlixian.fishandbearpaw.entity.Dto.CommentDto;
import com.chenlixian.fishandbearpaw.entity.Dto.CreateShareDto;
import com.chenlixian.fishandbearpaw.entity.Dto.ShareDto;
import com.chenlixian.fishandbearpaw.entity.Vo.CommentVo;
import com.chenlixian.fishandbearpaw.entity.Vo.GeneralVO;
import com.chenlixian.fishandbearpaw.entity.Vo.MessageVo;
import com.chenlixian.fishandbearpaw.entity.Vo.ShareVo;
import com.chenlixian.fishandbearpaw.entity.enumEntity.ErrorListEnum;
import com.chenlixian.fishandbearpaw.service.ShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author chenlixian
 * @description TODO 分享控制器
 * @date 2020/3/18 11:44
 */
@Controller
@RequestMapping("/share")
@CrossOrigin
public class ShareController {
    @Autowired
    private ShareService shareService;
    @RequestMapping("/queryShare")
    @ResponseBody
    public GeneralVO queryShare (@RequestBody ShareDto shareDto){
            List<ShareVo> shares = shareService.queryShare(shareDto);
            for (ShareVo shareVo : shares){
                shareVo.setCommentList(shareService.queryAllComment(shareVo.getId()));
            }
            return new GeneralVO(ErrorListEnum.E200,shares);
    }
    @RequestMapping("/vote")
    @ResponseBody
    public GeneralVO vote (@RequestBody Map<String,Object> map){
         Integer opUserId = (Integer) map.get("opUserId");
         Integer userId = (Integer) map.get("userId");
         Integer count = shareService.vote((String) map.get("imgId"),userId,opUserId);
         if (count != 1)
             return new GeneralVO(ErrorListEnum.E20006,"投票失败");
         else
             return new GeneralVO(ErrorListEnum.E200,"投票成功");
    }

    @RequestMapping("/addComment")
    @ResponseBody
    public GeneralVO addComment (@RequestBody CommentDto commentDto){
        Integer count = shareService.addComment(commentDto);
        if (count == 1)
            return new GeneralVO(ErrorListEnum.E200,"评论成功");
        else
            return new GeneralVO(ErrorListEnum.E30002,"评论失败");
    }
    @RequestMapping("/create")
    @ResponseBody
    public GeneralVO create (@RequestBody CreateShareDto createShareDto){
       return shareService.create(createShareDto);
    }
    @RequestMapping("/queryMessage")
    @ResponseBody
    public GeneralVO queryMessage (@RequestBody Map<String,Integer> map){
        Integer userId = map.get("userId");
        return new GeneralVO(ErrorListEnum.E200,shareService.queryMessage(userId));
    }
    @RequestMapping("/clearMessage")
    @ResponseBody
    public GeneralVO clearMessage (@RequestBody Map<String,Integer> map){
        Integer userId = map.get("userId");
        return new GeneralVO(ErrorListEnum.E200,shareService.clearMessage(userId));
    }
    @RequestMapping("/getMessage")
    @ResponseBody
    public GeneralVO getMessage (@RequestBody Map<String,Integer> map){
        Integer userId = map.get("userId");
        List<MessageVo> messageVos = shareService.getMessage(userId);
        return new GeneralVO(ErrorListEnum.E200,messageVos);
    }
}
